package cn.hrlly.tao2shou.callback;

import com.google.gson.Gson;
import com.zhy.http.okhttp.callback.Callback;

import cn.hrlly.tao2shou.R;
import cn.hrlly.tao2shou.domain.OrderList;
import cn.hrlly.tao2shou.listener.OnOrderListListener;
import okhttp3.Request;
import okhttp3.Response;

/**
 * User: gengliang
 * Date: 2016-01-31
 * Time: 14:20
 */

public class OrderListCallback extends Callback<OrderList> {

    private OnOrderListListener onOrderListListener;

    public OrderListCallback(OnOrderListListener onOrderListListener) {
        this.onOrderListListener = onOrderListListener;
    }

    @Override
    public OrderList parseNetworkResponse(Response response) throws Exception {
        String string=response.body().string();
        OrderList orderList=new Gson().fromJson(string,OrderList.class);
        return orderList;
    }

    @Override
    public void onError(Request request, Exception e) {
        onOrderListListener.showOrderFail(R.string.network_connection_error);
    }

    @Override
    public void onResponse(OrderList orderList) {
        if(orderList.getStatus()==0)
            onOrderListListener.showOrderSucc(orderList);
        else
            onOrderListListener.showOrderFail(orderList.getErrorMessage());
    }
}
